package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.Comment;
import com.cskaoyan.bean.pojo.CommentExample;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.beanwx.bo.WxCommentListBO;
import com.cskaoyan.beanwx.bo.WxCommentPostBO;
import com.cskaoyan.beanwx.vo.WxCommentCountVO;
import com.cskaoyan.beanwx.vo.WxCommentListVO;
import com.cskaoyan.beanwx.vo.WxCommentPostVO;
import com.cskaoyan.dao.CommentMapper;
import com.cskaoyan.dao.UserMapper;
import com.cskaoyan.utils.DateFormatUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 小程序评论服务层实现
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 获取评论列表
     *
     * @return WxCommentListVO
     */
    @Override
    public WxCommentListVO getCommentList(WxCommentListBO wxCommentListBO) {
        //分页
        PageHelper.startPage(wxCommentListBO.getPage(), wxCommentListBO.getSize());

        //查询函数
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(wxCommentListBO.getValueId());
        //criteria.andTypeEqualTo(wxCommentListBO.getType());

        //条件查询
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        //构造输出参数
        WxCommentListVO wxCommentListVO = new WxCommentListVO();
        ArrayList<WxCommentListVO.ItemsDTO> data = new ArrayList<>();
        for (Comment comment : comments) {
            WxCommentListVO.ItemsDTO itemsDTO = new WxCommentListVO.ItemsDTO();
            itemsDTO.setAddTime(DateFormatUtils.date2Str(comment.getAddTime()));
            itemsDTO.setContent(comment.getContent());
            itemsDTO.setPicList(comment.getPicUrls());
            WxCommentListVO.ItemsDTO.UserInfoVO userInfo = new WxCommentListVO.ItemsDTO.UserInfoVO();
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            userInfo.setAvatarUrl(user.getAvatar());
            userInfo.setNickName(user.getNickname());
            itemsDTO.setUserInfo(userInfo);
            data.add(itemsDTO);
        }
        wxCommentListVO.setData(data);
        wxCommentListVO.setCount(data.size());
        wxCommentListVO.setCurrentPage(wxCommentListBO.getPage());
        return wxCommentListVO;
    }

    /**
     * 增加评论
     *
     * @return WxCommentPostVO
     */
    @Override
    public WxCommentPostVO postComment(WxCommentPostBO wxCommentPostBO, Integer userId) {
        //插入
        Comment comment = new Comment();
        comment.setAddTime(DateFormatUtils.long2Date(System.currentTimeMillis()));
        comment.setContent(wxCommentPostBO.getContent());
        comment.setDeleted(false);
        comment.setHasPicture(wxCommentPostBO.getHasPicture());
        comment.setPicUrls(wxCommentPostBO.getPicUrls());
        comment.setStar(wxCommentPostBO.getStar());
        comment.setType(wxCommentPostBO.getType());
        comment.setUpdateTime(comment.getAddTime());
        comment.setValueId(Integer.parseInt(wxCommentPostBO.getValueId()));
        comment.setUserId(userId);
        commentMapper.insertSelective(comment);

        //回显
        WxCommentPostVO wxCommentPostVO = new WxCommentPostVO();
        wxCommentPostVO.setAddTime(DateFormatUtils.date2Str(comment.getAddTime()));
        wxCommentPostVO.setContent(comment.getContent());
        wxCommentPostVO.setHasPicture(comment.getContent());
        wxCommentPostVO.setId(comment.getId());
        wxCommentPostVO.setPicUrls(comment.getPicUrls());
        wxCommentPostVO.setStar(comment.getStar());
        wxCommentPostVO.setType(comment.getType());
        wxCommentPostVO.setUpdateTime(DateFormatUtils.date2Str(comment.getUpdateTime()));
        wxCommentPostVO.setUserId(comment.getUserId());
        wxCommentPostVO.setValueId(comment.getValueId());

        return wxCommentPostVO;
    }
    /**
     * 评论计数
     * @return WxCommentCountVO
     */
    @Override
    public WxCommentCountVO countComment(Integer valueId) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        Integer hasPicCount = 0;
        for (Comment comment : comments) {
            if(comment.getHasPicture())
                hasPicCount++;
        }
        WxCommentCountVO wxCommentCountVO = new WxCommentCountVO();
        wxCommentCountVO.setHasPicCount(hasPicCount);
        wxCommentCountVO.setAllCount(comments.size());
        return wxCommentCountVO;
    }
}
